package com.drojian;

import android.content.Context;
import android.graphics.Color;
import android.net.Uri;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.VideoView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.resource.bitmap.RoundedCorners;
import com.bumptech.glide.load.resource.drawable.DrawableTransitionOptions;
import com.bumptech.glide.request.RequestOptions;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

public class FileAdapter extends RecyclerView.Adapter<FileAdapter.FileViewHolder> {

    public static final String FILE_KEY = "file_key";
    private List<File> fileList = new ArrayList<>();
    private Context context;

    public void setNewData(List<File> fileList) {
        this.fileList.clear();
        this.fileList.addAll(fileList);
        notifyDataSetChanged();
    }

    @NonNull
    @Override
    public FileViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        context = parent.getContext();
        View view;
        if (viewType == 0) {
            view = LayoutInflater.from(context).inflate(R.layout.list_item_image, parent, false);
        } else {
            view = LayoutInflater.from(context).inflate(R.layout.list_item_vedio, parent, false);
        }
        return new FileViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull FileViewHolder holder, int position) {
        File file = fileList.get(position);
        String miniType = FileUtils.getMiniType(file);
        if (!TextUtils.isEmpty(miniType)) {
            if (miniType.startsWith("image") || miniType.startsWith("gif")) {
                Glide.with(context)
                        .load(file.getAbsolutePath())
                        .transition(DrawableTransitionOptions.withCrossFade())
                        .apply(RequestOptions.bitmapTransform(new RoundedCorners(1000)))
                        .centerCrop()
                        .into(holder.imageview);
            } else if (miniType.startsWith("video")) {
                holder.video.setVideoURI(Uri.parse(file.getAbsolutePath()));
            }
        }

        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (!TextUtils.isEmpty(miniType)) {
                    if (miniType.startsWith("audio")) {
                        // AudioPlayerActivity.start(context, file.getAbsolutePath());
                    } else {
                        //VideoPlayerActivity.start(context, file.getAbsolutePath());
                    }
                }
            }
        });

    }

    @Override
    public int getItemCount() {
        return fileList.size();
    }

    @Override
    public int getItemViewType(int position) {
        File file = fileList.get(position);
        String miniType = FileUtils.getMiniType(file);
        return miniType.startsWith("video") ? 1 : 0;
    }

    public static class FileViewHolder extends RecyclerView.ViewHolder {
        private final ImageView imageview;
        private final VideoView video;

        public FileViewHolder(@NonNull View itemView) {
            super(itemView);
            imageview = itemView.findViewById(R.id.imageview);
            video = itemView.findViewById(R.id.video);

        }
    }
}
